package com.isunland.managesystem.base;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnShowListener;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import com.isunland.managesystem.R;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

public class BaseMonthTimeDialogFragment extends DialogFragment {
  private static DateFormat sFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
  protected static final String EXTRA_START_DATE =
      "com.isunland.managesystem.base.BaseMonthTimeDialogFragment.extrar_start_date";
  protected static final String EXTRA_TITLE_ID = "com.isunland.managesystem.ui.extra_title_id";
  public static final String EXTRA_DATE = "com.isunland.managesystem.ui.extra_date";
  protected static final String EXTRA_TITLE = "com.isunland.managesystem.ui.extra_title";
  private AlertDialog mAlertDialog;
  private Date mDate;// 回传的日期
  private String mTitle;

  /**
   * 创建日期Dialog
   *
   * @param startDate 显示的日期
   * @param titleId Dialog标题的id.
   */
  private static BaseMonthTimeDialogFragment newInstance(Date startDate, int titleId,
      String title) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_START_DATE, startDate);
    if (titleId != 0) {

      args.putInt(EXTRA_TITLE_ID, titleId);
    }
    if (title != null) {

      args.putString(EXTRA_TITLE, title);
    }
    BaseMonthTimeDialogFragment fragment = new BaseMonthTimeDialogFragment();
    fragment.setArguments(args);
    return fragment;
  }

  public static BaseMonthTimeDialogFragment newInstance(Date startDate, int titleId) {
    return newInstance(startDate, titleId, null);
  }

  public static BaseMonthTimeDialogFragment newInstance(Date startDate, String title) {
    return newInstance(startDate, 0, title);
  }

  public static BaseMonthTimeDialogFragment newInstance(Date startDate) {
    return newInstance(startDate, 0, null);
  }

  @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
    // 初始化DatePickerTimePicker
    Calendar calendar = Calendar.getInstance();
    Date date;
    if (getArguments() != null) {
      date = (Date) getArguments().getSerializable(EXTRA_START_DATE);
      if (date == null) {
        date = new Date();
      }
      mTitle = initTitle();
      if (mTitle == null) {
        mTitle = "请选择时间";
      }
    } else {
      date = new Date();
      mTitle = "请选择时间";
    }

    calendar.setTime(date);

    int year = calendar.get(Calendar.YEAR);
    int month = calendar.get(Calendar.MONTH);
    int day = calendar.get(Calendar.DAY_OF_MONTH);
    View view =
        getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog_time_no_day, null);
    final DatePicker datePicker =
        (DatePicker) view.findViewById(R.id.dp_date_chooseYMDTimeDialogFragment);
    //隐藏 日 控件
    //		((ViewGroup)((ViewGroup) datePicker.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
    datePicker.findViewById(Resources.getSystem().getIdentifier("day", "id", "android"))
        .setVisibility(View.GONE);
    datePicker.init(year, month, day, null);
    // 初始化时间的dialog
    mAlertDialog = new AlertDialog.Builder(getActivity()).setTitle(mTitle).setView(view)
        //				mAlertDialog = new AlertDialog.Builder(getActivity())
        //				.setTitle(R.string.chooseAcquireTime).setView(widget)
        .setPositiveButton(android.R.string.ok, null).create();
    mAlertDialog.setOnShowListener(new OnShowListener() {

      @Override public void onShow(DialogInterface arg0) {
        Button b = mAlertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        b.setOnClickListener(new OnClickListener() {

          @Override public void onClick(View arg0) {
            mDate = new GregorianCalendar(datePicker.getYear(), datePicker.getMonth(),
                datePicker.getDayOfMonth()).getTime();
            setResult(Activity.RESULT_OK);
          }
        });
      }
    });

    return mAlertDialog;
  }

  /**
   *
   */
  private String initTitle() {
    String titleFinal = null;
    int titleId = getArguments().getInt(EXTRA_TITLE_ID);
    String title = getArguments().getString(EXTRA_TITLE);
    if (title != null) {
      titleFinal = title;
    } else {
      try {
        titleFinal = getString(titleId);
      } catch (Exception e) {
        // Auto-generated catch block
        e.printStackTrace();
      }
    }
    if (TextUtils.isEmpty(titleFinal)) {
      titleFinal = "请选择时间";
    }
    return titleFinal;
  }

  /**
   * 将用户选择的日期返回.
   */
  protected void setResult(int resultCode) {
    if (getTargetFragment() == null) return;
    Intent i = new Intent();
    i.putExtra(EXTRA_DATE, mDate);
    getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, i);
    mAlertDialog.dismiss();
  }
}
